package org.chuanyang.mm;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public abstract class EditBaseActivity extends Activity {

	protected TextView showTag;
	protected TextView show;
	protected EditText edit;
	protected Button submit;

	final int[] mResizeModeValues = new int[] {
			WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED,
			WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE,
			WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN,
			WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING, };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(setLayout());
		getWindow().setSoftInputMode(mResizeModeValues[1]);

		showTag = (TextView) findViewById(R.id.tv_operate_edit_show_tag);
		show = (TextView) findViewById(R.id.tv_operate_edit_show);
		edit = (EditText) findViewById(R.id.tv_operate_edit_in);
		submit = (Button) findViewById(R.id.btn_operate_submit);
		
		initView();
		
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		if (bundle != null) {
			refreshShow(bundle.getString(setReceiveTag()));
			edit.setText(bundle.getString(setReceiveTag()));
		}
		
		edit.setSelection(edit.length());
		
		edit.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				refreshShow(edit.getText().toString());
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});

		submit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				submitComent();
			}

		});
	}

	private void refreshShow(String str) {
		show.setText(str);
	}

	private void submitComent() {
		Intent data = new Intent();
		data.putExtra(setSendTag(), show.getText().toString());
		setResult(RESULT_OK, data);
		
		this.finish();
	}
	
	abstract int setLayout();
	abstract void initView();
	abstract String setReceiveTag();
	abstract String setSendTag();


}
